package com.yxs.wechatdemo.service.impl;

import com.google.gson.Gson;
import com.yxs.wechatdemo.config.WeChatConfig;
import com.yxs.wechatdemo.service.WeChatService;
import com.yxs.wechatdemo.utils.SignUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * 1 - 微信网页授权 getUserInfo
 * 2 - JSSDK票证获取 getSignInfo
 * Created by Administrator on 2018/10/9 0009.
 */
@Service
@Slf4j
public class WeChatServiceImpl implements WeChatService {

    private final static String JSSDK_ACCESS_TOKEN = "JSDK";


    @Autowired
    private WeChatConfig weChatConfig;

    @Autowired
    private RedisServiceImpl redisService;

    private Gson gson = new Gson();

    private RestTemplate restTemplate = new RestTemplate();

    public Map getAccessToken() {
        String url = String.format(GET_ACCESS_TOKEN_URL, weChatConfig.getMpAppId(), weChatConfig.getMpAppSecret());
        return restTemplate.getForObject(url, Map.class);
    }

    public Map getTicket() {
        Map<String, Object> map = getAccessToken();
        String url = String.format(GET_JSAPI_TICKET_URL, map.get("access_token"));
        return restTemplate.getForObject(url, Map.class);
    }

    public Map getSignInfo(String url) {
        String value = redisService.getRedis(JSSDK_ACCESS_TOKEN);
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isEmpty(value)) {
            map = gson.fromJson(value, Map.class);
        } else {
            map = getTicket();
            redisService.setRedis(JSSDK_ACCESS_TOKEN, gson.toJson(map));
        }
        return SignUtil.getSign((String) map.get("ticket"), url, weChatConfig.getMpAppId());
    }

    public String getAuthorize(String returnUrl) {
        return String.format(CONNECT_OAUTH2_AUTHORIZE_URL, weChatConfig.getMpAppId(), weChatConfig.getRedirectUri(), weChatConfig.getScope(), returnUrl);
    }

    public String getOauth2AccessToken(String code) {
        String url = String.format(OAUTH2_ACCESS_TOKEN_URL, weChatConfig.getMpAppId(), weChatConfig.getMpAppSecret(), code);
        return restTemplate.getForObject(url, String.class);
    }

    public String getTokenUrl(String code, String returnUrl) {
        String token = "";
        String jsonStr = getOauth2AccessToken(code);
        Map<String, Object> map = gson.fromJson(jsonStr, Map.class);
        String url = String.format(OAUTH2_USERINFO_URL, map.get("access_token"), map.get("openid"), weChatConfig.getLang());
        String result = restTemplate.getForObject(url, String.class);
        try {
            result = new String(result.getBytes("ISO-8859-1"), "utf-8");
            token = redisService.setRedis(result);
        } catch (Exception e) {

        }
        return weChatConfig.getTokenUrl() + returnUrl + "?token=" + token;
    }


}
